﻿#define  _CRT_SECURE_NO_WARNINGS 1
#include"标头.h"


void CreateMap()
{
	int i = 0;
	SetPos(0, 0);
	for (i = 0; i <= 58; i++)
	{
		SetPos(i, 0);
		wprintf(L"%c", P);
		SetPos(i, 26);
		wprintf(L"%c", P);
	}
	for (i = 1; i < 26; i++)
	{
		SetPos(0, i);
		wprintf(L"%c", Q);
		SetPos(58, i);
		wprintf(L"%c", Q);
	}
	SetPos(29, 27);

}

 void WelcomeToGame()
{
	setlocale(LC_ALL, "");
	system("mode con cols=90 lines=33");
	system("title 贪吃蛇");

	SetPos(45, 13);
	printf("欢迎来到贪吃蛇游戏");
	SetPos(45, 17);
	system("pause");
	system("cls");
	SetPos(43, 12);
	printf("w,s,a,d,分别控制蛇的移动， F3为加速，F4为减速\n");
	SetPos(43, 13);
	printf("加速可以获得更多的枫树");
	SetPos(43, 20);
	system("pause");
	system("cls");

}
void GameStart(pSnake ps)
{
	system("mode con cols=100 lines=30");
	system("title 贪吃蛇");
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(hOutput, &CursorInfo);//获取控制台光标信息 
	CursorInfo.bVisible = false; //隐藏控制台光标 
	SetConsoleCursorInfo(hOutput, &CursorInfo);//设置控制台光标状态 
	WelcomeToGame();
	CreateMap();
}

int KillByWall(pSnake ps)
{
	if ((ps->_pSnake->x == 0)
		|| (ps->_pSnake->x == 56)
		|| (ps->_pSnake->y == 0)
		|| (ps->_pSnake->y == 26))
	{
		ps->_Status = KILL_BY_WALL;
		return 1;
	}
	return 0;
}

int KillBySelf(pSnake ps)
{
	pSnakeNode cur = ps->_pSnake->next;
	while (cur)
	{
		if ((ps->_pSnake->x == cur->x)
			&& (ps->_pSnake->y == cur->y))
		{
			ps->_Status = KILL_BY_SELF;
			return 1;
		}
		cur = cur->next;
	}
	return 0;
}


void NoFood(pSnakeNode next, pSnake ps)
{
	next->next = ps->_pSnake;
	ps->_pSnake = next;
	pSnakeNode cur = next;
	while (cur->next->next)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", Q);
		cur = cur->next;
	}

	SetPos(cur->next->x, cur->next->y);
	printf("  ");
	free(cur);
	cur->next = NULL;
}


void EatFood(pSnakeNode next, pSnake ps)
{
	next->next = ps->_pSnake;
	ps->_pSnake = next;

	pSnakeNode cur = next->next;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", Q);
		cur = cur->next;
	}

	ps->_Socre += ps->_foodWeight;
	free(cur);//要记得去销毁这个食物

	CreatFood(ps);

}

int NextIsFood(pSnakeNode next, pSnake ps)
{
	return ((next->x == ps->_pFood->x) && (next->y == ps->_pFood->y));
}

void SnakeMove(pSnake ps)
{
	pSnakeNode next = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (next == NULL)
	{
		perror("creat next fail");
		return;
	}

	switch (ps->_Status)
	{
	case UP:
		next->x = ps->_pSnake->x;
		next->y = ps->_pSnake->y - 1;
		break;
	case DOWN:
		next->x = ps->_pSnake->x;
		next->y = ps->_pSnake->y + 1;
		break;
	case LEFT:
		next->x = ps->_pSnake->x - 1;
		next->y = ps->_pSnake->y;
		break;
	case RIGHT:
		next->x = ps->_pSnake->x + 1;
		next->y = ps->_pSnake->y;
		break;
	}
	if (NextIsFood( next, ps))
		{
		EatFood(next,ps);
		}
	else
	{
		NoFood(next, ps);
	}
	KillByWall(ps);
	KillBySelf(ps);


}
void pause()
{
	while (1)
	{
		Sleep(300);
		if (KEY_PRESS(VK_SPACE))
		{
			return;
		}
	}
}




void InitSnake(pSnake ps)
{
	pSnakeNode cur = NULL;
	for (int i = 0; i < 5; i++)
	{
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("Snake creak fail");
			return;
		}


		cur->x = X + i;
		cur->y = Y;
		cur->next = NULL;

		//开始插入
		if (ps->_pSnake = NULL)
		{
			ps->_pSnake = cur;
		}
		else
		{
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}
		//开始打印
		while (cur)
		{
			SetPos(cur->x, cur->y);
			wprintf(L"%c", WALL);
			cur = cur->next;
		}

	}
	SetPos(29, 27);
	ps->_Status = OK;
	ps->_Dir = RIGHT;
	ps->_Socre = 0;
	ps->_foodWeight = 10;
	ps->_SleepTime = 200;
}




void PrintHelpInfo()
{
	SetPos(67, 15);
	printf("不能穿墙，不能咬到自己\n");
	SetPos(67, 16);
	printf("用w,a,s,d分别控制蛇的移动\n");
	SetPos(67, 17);
	printf("F3加速，F4为减速\n");
	SetPos(67, 18);
	printf("ESC ：退出游戏.space：暂停游戏\n");
	SetPos(67, 28);
}

void Gamerun(pSnake ps)
{
	PrintHelpInfo();
	do
	{
		SetPos(67,13);
		printf("您当前的枫树为%d",ps->_Socre);
		SetPos(67, 14);
		printf("当前的食物可得枫树为%d",ps->_foodWeight);
		if (KEY_PRESS(0x57) && ps->_Dir != DOWN)
		{
			ps->_Dir = UP;
		}
		else if(KEY_PRESS(0x53)&&ps->_Dir!=UP)
		{
			ps->_Dir = DOWN;
		}
		else if (KEY_PRESS(0x41)&&ps->_Dir!=LEFT)
		{
			ps->_Dir = RIGHT;
		}
		else if (KEY_PRESS(0x44) && ps->_Dir != RIGHT)
		{
			ps->_Dir = LEFT;
		}
		else if (KEY_PRESS(VK_F3))
		{
			if (ps->_SleepTime >= 80)
			{
				ps->_SleepTime -= 30;
				ps->_Socre += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			if (ps->_SleepTime <= 320)
			{
				ps->_SleepTime += 30;
				ps->_Socre -= 2;
			}
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			pause();
		}
		Sleep(ps->_SleepTime);
		SnakeMove( ps);
	} while (ps->_Status==OK);
}

void GameEnd(pSnake ps)
{
	pSnakeNode cur = ps->_pSnake;
	SetPos(24, 12);
	switch (ps->_Status)
	{
	case END_NOMAL:
		printf("您主动退出游戏\n");
		break;
	case KILL_BY_SELF:
		printf("您撞上⾃⼰了 ,游戏结束!\n");
		break;
	case KILL_BY_WALL:
		printf("您撞墙了,游戏结束!\n");
		break;
	}
	//释放蛇⾝的节点 
	while (cur)
	{
		pSnakeNode del = cur;
		cur = cur->next;
		free(del);
	}
}


int main()
{


		int ch = 0;
		srand((unsigned int)time(NULL));
		do
		{
			Snake snake = { 0 };
			GameStart(&snake);
			GameEnd(&snake);
			SetPos(20, 15);
			printf("再来⼀局吗？(Y/N):");
			ch = getchar();
			getchar();//清理\n 
		} while (ch == 'Y' || ch == 'y');
		SetPos(0, 27);


	Snake ps = { 0 };

	InitSnake(&ps);
	CreatFood(&ps);
	PrintHelpInfo();
	Gamerun(&ps);
	GameEnd(&ps);
	return 0;
}
